package com.repast.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.repast.common.ServerResponse;
import com.repast.pojo.Address;
import com.repast.pojo.Student;
import com.repast.service.IAddressService;
import com.repast.utils.DateUtils;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/address/")
public class AddressController {
	
	@Autowired
	private IAddressService addressService;
	
	//查询用户的所有地址列表,地址状态为1的为默认地址,每个用户只能有一个默认的地址
	//根绝openid展示地址列表
	//0地址显示成功,1002还没有添加任何地址
	@RequestMapping("list.do")
	@ResponseBody
	public ServerResponse selectAddressList(String openid){
		return addressService.selectAddressList(openid);
	}
	
	//修改默认地址,就是把选中的地址状态设为1,其他全部设为0
	//状态返回,表示修改地址状态成功
	@RequestMapping("changeStatus.do")
	@ResponseBody
	public ServerResponse changeStatus(String openid,Integer addressId){
		return addressService.changeStatus(openid, addressId);
	}
	
	//添加地址,@requestBody注解接收数据,传过来的json数据里面的key必须和address里面字段的属性一一对应
	//0地址插入成功,1003地址添加失败
	@RequestMapping("add.do")
	@ResponseBody
	public ServerResponse addAddress(String json) throws Exception{
		Address address = new Address();
		JSONObject jsonStr = JSONObject.fromObject(json);
		address.setAddressAddress(jsonStr.getString("addressAddress"));
		address.setOpenid(jsonStr.getString("openid"));
		address.setAddressStatus((jsonStr.getInt("addressStatus")));
		address.setAddressPhone(jsonStr.getString("addressPhone"));
		address.setAddressName(jsonStr.getString("addressName"));
		String formatDateTime = DateUtils.formatDateTime(new Date(), "long");
		address.setAddressCreateTime(formatDateTime);
		address.setAddressUpdateTime(formatDateTime);
		return addressService.insertAddress(address);
	}
	
	//删除地址,
	//0删除地址成功,1004删除地址失败
	@RequestMapping("delete.do")
	@ResponseBody
	public ServerResponse deleteAddress(Integer addressId){
		return addressService.deleteAddress(addressId);
	}
	
	//编辑地址,@requestBody注解接收数据,传过来的json数据里面的key必须和address里面字段的属性一一对应
	//0代表编辑地址成功,1005编辑/修改地址失败
	@RequestMapping("update.do")
	@ResponseBody
	public ServerResponse updateAddress(String json) throws Exception{
		Address address = new Address();
		JSONObject jsonStr = JSONObject.fromObject(json);
		address.setAddressAddress(new String(jsonStr.getString("addressAddress").getBytes("iso-8859-1"),"utf-8"));
		address.setOpenid(jsonStr.getString("openid"));
		address.setAddressStatus((jsonStr.getInt("addressStatus")));
		address.setAddressPhone(new String(jsonStr.getString("addressPhone").getBytes("iso-8859-1"),"utf-8"));
		address.setAddressName(new String(jsonStr.getString("addressName").getBytes("iso-8859-1"),"utf-8"));
		String formatDateTime = DateUtils.formatDateTime(new Date(), "long");
		address.setAddressCreateTime(formatDateTime);
		address.setAddressUpdateTime(formatDateTime);
		return addressService.updateAddress(address);
	}
	
	//根据openid查询用户的默认收货信息
	//0表示查询默认地址正确,1006表示没有添加任何默认的收货地址,1007系统错误,出现多个默认地址,请联系管理员
	@RequestMapping("delivery_info.do")
	@ResponseBody
	public ServerResponse selectDeliveryInfoByOpenid(String openid){
		return addressService.selectDeliveryInfoByOpenid(openid);
	}
	
	@RequestMapping("json.do")
	@ResponseBody
	public String json(@RequestBody Student student){
		System.out.println(student);
		return student.toString();
	}
}
